package dianmobile.eistarandroid.ui.fragments;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import dianmobile.eistarandroid.ByhhAndroidApplication;
import dianmobile.eistarandroid.R;
import dianmobile.eistarandroid.beans.BoardItemEntity;
import dianmobile.eistarandroid.network.request.RequestCallback;
import dianmobile.eistarandroid.network.request.SubscriptionListRequest;
import dianmobile.eistarandroid.ui.Adapter.BoardListAdapter;
import dianmobile.eistarandroid.ui.activities.BoardContentActivity;
import dianmobile.eistarandroid.ui.views.BoardContentListView;
import dianmobile.eistarandroid.utils.ConstantsData;
import zrc.widget.SimpleHeader;
import zrc.widget.ZrcListView;


public class BookmarkFragment extends Fragment {

    /**订阅列表*/
    private BoardListAdapter boardListAdapter;
    private List<BoardItemEntity> subscriptListData = new ArrayList<>();    //TODO 从缓存中取或者从application中取
    private ZrcListView subscriptListView;

    private ViewPager boardContentVp;
    private List<View> pagerContentView;

    private View thisView;
    private LayoutInflater inflater;

    private ByhhAndroidApplication application;

    private BoardContentListView top10ListView;
    private BoardContentListView recommendationListView;

    /**
     * rewrite by yy: add judge of thisView is null, to avoid another init of thisView
     * @param inflater
     * @param container
     * @param saveInstanceSt
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceSt) {
        this.inflater = inflater;
        if (thisView == null) {
            thisView = inflater.inflate(R.layout.fragment_bookmark, container, false);
        }
        //initView();
        return thisView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstance) {
        super.onActivityCreated(savedInstance);
        application = (ByhhAndroidApplication)getActivity().getApplication();
        initView();
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        /* *** 注释这段代码的原因 ***
         * 根据Fragment的生命周期可以知道,只有当宿主Activity的onStop()函数调用之后,Fragment的onStop()函数
         * 才会调用.
         * 需要注意的是这个Fragment是和其他Fragment放在FragmentManager进行管理的,都处于MainActivity中,也就是
         * 说MainActivity的Fragment切换事件不会回调改函数,因此也就不应该在这次保存Fragment的状态.
         * 在MainActivity中的任何Fragment跳转到别的Activity都会因此此函数回调.
         * 应该将次Fragment的状态保存放到MainActivity中去.
         */
//        CacheDataAcquirer acquire = CacheManager.getCacheDataAcquirer();
//        // 缓存十大的数据
//        acquire.cacheData(getActivity().getApplicationContext(), ConstantsData.URL_TOP_10);
//        // 缓存推荐的内容
//        acquire.cacheData(getActivity().getApplicationContext(), ConstantsData.URL_RECOMENDATION);

        super.onStop();
    }

    @Override
    public void onDestroyView(){
        super.onDestroyView();
    }

    private void initView() {

        boardContentVp = (ViewPager) thisView.findViewById(R.id.vp_board_content);
        pagerContentView = new ArrayList<>();
        pagerContentView.add(getSubscriptListView());
        boardContentVp.setAdapter(viewPagerAdapter);
    }


    private View getSubscriptListView() {
        subscriptListView = new ZrcListView(thisView.getContext());

        SimpleHeader header = new SimpleHeader(thisView.getContext());
        header.setTextColor(0xff0066aa);
        header.setCircleColor(0xff33bbee);
        subscriptListView.setHeadable(header);
        subscriptListView.setOnRefreshStartListener(new ZrcListView.OnStartListener() {
            @Override
            public void onStart() {
                getSubscriptListData();
            }
        });

        boardListAdapter = new BoardListAdapter(thisView.getContext(), subscriptListData);
        subscriptListView.setAdapter(boardListAdapter);
        getSubscriptListData();

        subscriptListView.setOnItemClickListener(new ZrcListView.OnItemClickListener() {
            @Override
            public void onItemClick(ZrcListView parent, View view, int position, long id) {
                BoardItemEntity item = subscriptListData.get(position);
                Intent intent = new Intent(getActivity(), BoardContentActivity.class);
                intent.putExtra(ConstantsData.EXTRA_BOARD_NAME, item.getBoardName());
                startActivity(intent);
            }
        });

        return subscriptListView;
    }

    private void getSubscriptListData() {
        SubscriptionListRequest.execute(thisView.getContext(), new RequestCallback() {
            @Override
            public void onResult(Map<String, Object> result, String error) {
                if (error == null) {
                    subscriptListData.clear();
                    subscriptListData.addAll((ArrayList<BoardItemEntity>) result.get("result_data"));
                    boardListAdapter.notifyDataSetChanged();
                    subscriptListView.setRefreshSuccess();
                    application.setSubscriptionList(subscriptListData);
                } else {
                    subscriptListView.setRefreshFail("网络错误");
                }
            }
        });
    }



    private PagerAdapter viewPagerAdapter = new PagerAdapter() {

        @Override
        public int getCount() {
            return pagerContentView.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(pagerContentView.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(pagerContentView.get(position), 0);
            return pagerContentView.get(position);
        }
    };

}
